'use strict';

const BaseModel = require('./BaseModel');

/**
 * 用户角色
 */
class AdminRolesModel extends BaseModel {

  constructor() {
    super('admin_roles', 'id');

    this.entity = class entity {
      constructor() {
        this.id = null;
        this.state = null;
        this.roleName = null;
        this.roleDes = null;
        this.creator = null;
        this.createTime = null;
        this.updateTime = null;
      }
    };
  }

  async selectAdminRolesListCount(app, name) {
    const sqls = [];
    sqls.push(`select count(1) as dataCount from ${this.tableName} where 1=1 and state = 1 `);
    if (name) sqls.push(` and role_name like \'%${name}%\' `);
    sqls.push(' order by create_time desc ');
    const sql = sqls.join('');
    app.logger.debug('SQL:', sql);
    const results = await app.mysql.query(sql);
    if (results && results.length > 0) {
      return results[0].dataCount;
    }
    return 0;
  }

  async selectAdminRolesList(app, wheres) {
    const sqls = [];
    sqls.push(`select * from ${this.tableName} where 1=1 and state = 1 `);
    if (wheres.name) {
      sqls.push(` and role_name like \'%${wheres.name}%\' `);
    }
    sqls.push(` order by create_time desc limit ${(wheres.pageIndex - 1) * wheres.pageSize},${wheres.pageSize}`);
    const sql = sqls.join('');
    app.logger.debug('SQL:', sql);
    
    const results = await app.mysql.query(sql);
    if (results) {
      const adminRolesList = [];
      for (const dbentity of results) {
        let adminRoles = new this.entity();
        adminRoles = this.transformation_Entity(dbentity);
        adminRolesList.push(adminRoles);
      }
      return adminRolesList;
    }
    return null;
  }
  
}

module.exports = AdminRolesModel;

